﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;

namespace VSM
{
    public partial class Popup_Customer : System.Web.UI.Page
    {

        private static string CardCode = "";
        private static string dbType;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                dbType = Session[VSM.Utils.AppConstants.ID_Dealer].ToString() == "VSM" ? VSM.Utils.AppConstants.VSM : VSM.Utils.AppConstants.DEALER;
                //int RequestType = Request.QueryString["requestType"] == null ? 0 : Int32.Parse(Request.QueryString["requestType"]);
                CardCode = Request.QueryString["CustomerCode"] == null ? "" : Request.QueryString["CustomerCode"];
                disenaControl(false);
                LoadCustomer();
                btn_Save.Visible = false;
                btn_Modify.Visible = false;
            }
        }

        private void disenaControl(bool eod) {
            txt_CusName.Enabled = eod;
            txt_OtherName.Enabled = eod;
            txt_Email.Enabled = eod;
            txt_Tel.Enabled = eod;
            txt_Fax.Enabled = eod;
            txt_Mobile.Enabled = eod;
            txt_ContactPerson.Enabled = eod;
            txt_PhoneContact.Enabled = eod;
            txt_TaxCode.Enabled = eod;
            txt_Address.Enabled = eod;
            txt_Remarks.Enabled = eod;
        }

        private void LoadCustomer() {
            if (CardCode == "")
                return;
          MasterData masterDataWS = new MasterData();
           DataSet ds = masterDataWS.GetCustomerByCode("VSM", CardCode);
           if (ds.Tables[0].Rows.Count > 0)
           {
               CardCode = ds.Tables[0].Rows[0]["CardCode"].ToString();
               txt_CusID.Text = ds.Tables[0].Rows[0]["CardCode"].ToString();
               txt_CusName.Text = ds.Tables[0].Rows[0]["CardName"].ToString();
               txt_OtherName.Text = ds.Tables[0].Rows[0]["CardFName"].ToString();
               txt_Email.Text = ds.Tables[0].Rows[0]["E_Mail"].ToString();
               txt_Tel.Text = ds.Tables[0].Rows[0]["Phone1"].ToString();
               txt_Fax.Text = ds.Tables[0].Rows[0]["Fax"].ToString();
               txt_Mobile.Text = ds.Tables[0].Rows[0]["Cellular"].ToString();
               txt_ContactPerson.Text = ds.Tables[0].Rows[0]["U_ContactPerson"].ToString();
               txt_PhoneContact.Text = ds.Tables[0].Rows[0]["U_ContactPhone"].ToString();
               txt_TaxCode.Text = ds.Tables[0].Rows[0]["LicTradNum"].ToString();
               txt_Address.Text = ds.Tables[0].Rows[0]["Address"].ToString();
               txt_Remarks.Text = ds.Tables[0].Rows[0]["Notes"].ToString();
               btn_Modify.Visible = true;
           }
           else {

               txt_CusName.Text = "";
               txt_OtherName.Text = "";
               txt_Email.Text = "";
               txt_Tel.Text = "";
               txt_Fax.Text = "";
               txt_Mobile.Text = "";
               txt_ContactPerson.Text = "";
               txt_PhoneContact.Text = "";

               txt_TaxCode.Text = "";
               txt_Address.Text = "";
               txt_Remarks.Text = "";
              
               lb_Message.Text = "No result found...";
           }
        }
        protected void btn_New_Click(object sender, EventArgs e)
        {
            CardCode = "";
            txt_CusID.Text = "";
            txt_CusName.Text = "";
            txt_OtherName.Text = "";
            txt_Email.Text = "";
            txt_Tel.Text = "";
            txt_Fax.Text = "";
            txt_Mobile.Text = "";
            txt_ContactPerson.Text = "";
            txt_PhoneContact.Text = "";
            txt_TaxCode.Text = "";
            txt_Address.Text = "";
            txt_Remarks.Text = "";
            disenaControl(true);
            btn_Save.Visible = true;
        }

        public string CollectData()
        {
            try
            {
                DataTable dt = new DataTable("OCRD");
                dt.Columns.Add("CardCode");
                dt.Columns.Add("CardName");
                dt.Columns.Add("CardFName");

                dt.Columns.Add("E_Mail");
                dt.Columns.Add("Phone1");
                dt.Columns.Add("Fax");
                dt.Columns.Add("Cellular");
                
                dt.Columns.Add("U_ContactPerson");
                dt.Columns.Add("U_ContactPhone");

                dt.Columns.Add("Address");

                dt.Columns.Add("LicTradNum");
                dt.Columns.Add("Notes");

                dt.Columns.Add("CardType");
                dt.Columns.Add("GroupCode");

                DataRow dr = dt.NewRow();

                dr["CardCode"] = txt_CusID.Text;
                dr["CardName"] = txt_CusName.Text;
                dr["CardFName"] = txt_OtherName.Text;
                dr["Notes"] = txt_Remarks.Text;

                dr["Address"] = txt_Address.Text;
                dr["LicTradNum"] = txt_TaxCode.Text;
                dr["Phone1"] = txt_Tel.Text;

                dr["GroupCode"] = "103";
                dr["Cellular"] = txt_Mobile.Text;
                dr["Fax"] = txt_Fax.Text;
                dr["E_Mail"] = txt_Email.Text;

                dr["U_ContactPerson"] = txt_ContactPerson.Text;
                dr["U_ContactPhone"] = txt_PhoneContact.Text;
                dr["CardType"] = "C";

                dt.Rows.Add(dr);

    
                DocumentXML objInfo = new DocumentXML();
                DataSet ds = new DataSet("DS");
                ds.Tables.Add(dt.Copy());

                return objInfo.ToXMLStringFromDS(((ulong)DocType.BUSINESS_PARTNER).ToString(), ds);
            }
            catch (Exception ex)
            {
                ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "OKErrors", "Main.setMasterMessage('" + GeneralFunctions.UrlFullEncode(ex.ToString()) + "','');", true);
                ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "CloseLoading", "Dialog.hideLoader();", true);
                return string.Empty;
            }
        }


        private void AddCustomer() {
            try
            {
                Transaction ts = new Transaction();
                string xml = CollectData();
                DataSet ds = ts.CreateMarketingDocument(xml, "2", "0", false, "VSM");
                string erro = ds.Tables[0].Rows[0]["ErrMsg"].ToString();
                CardCode = txt_CusID.Text.Trim();
                lb_Message.Text = "[Add New:] " + erro;
            }
            catch (Exception ex) {
                lb_Message.Text = "[Add New:] " + ex.Message;
            }
        }

        private void UpdateCustomer() {
            try
            {
                Transaction ts = new Transaction();
                string xml = CollectData();
                DataSet ds = ts.CreateMarketingDocument(xml, "2", CardCode, true, "VSM");
                string erro = ds.Tables[0].Rows[0]["ErrMsg"].ToString();
                lb_Message.Text = "[Update:] " + erro;
            }
            catch (Exception ex) {
                lb_Message.Text = ex.Message;
            }
        }

        protected void btn_Save_Click(object sender, EventArgs e)
        {
            if (CardCode == "")
                AddCustomer();
            else
                UpdateCustomer();
        }

        protected void btn_Search_Click(object sender, EventArgs e)
        {
            if (txt_CusID.Text.Trim() == "")
                lb_Message.Text = "Please input customer code...";
            else
            {
                CardCode = txt_CusID.Text.Trim();
                LoadCustomer();
            }
        }

        protected void btn_Choose_Click(object sender, EventArgs e)
        {

        }

        protected void btn_Modify_Click(object sender, EventArgs e)
        {
            disenaControl(true);
            btn_Save.Visible = true;
        }
    }
}